home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Solitaire / Sources / Pyramid / GameCardPileDelegate.m < prev    next >
Text File  |  1994-01-17  |  6KB  |  213 lines

  1. /* indent:4  tabsize:8  font:fixed-width */
  2.  
  3. #import "GameCardPileDelegate.h"
  4. #import "../Solitaire/CardSet.subproj/cardset.h"
  5. #import "Pyramid.h"
  6.  
  7. @implementation GameCardPileDelegate
  8.  
  9. /*--------------------------------------------------------------------------
  10. |
  11. |    - setDiscardLeft:left discardRight:right
  12. |    returns: (id) self
  13. |
  14. |---------------------------------------------------------------------------
  15. |
  16. |    Set connections to other piles.
  17. |    
  18. \--------------------------------------------------------------------------*/
  19.  
  20. - setDiscardLeft:left discardRight:right
  21. {
  22.     discardCardPileViewL = left;
  23.     discardCardPileViewR = right;
  24.     return self;
  25. }
  26.  
  27.  
  28. /*--------------------------------------------------------------------------
  29. |
  30. |    - (BOOL)draggedPile:aCardPile from:gameCardPileView
  31. |    returns: (BOOL) YES if it is legal to drag the card.
  32. |
  33. |             (BOOL) NO if it is not legal to drag the card.
  34. |
  35. |---------------------------------------------------------------------------
  36. |
  37. |    Called by a GamePileCardView when the user tries to drag a card.
  38. |    
  39. \--------------------------------------------------------------------------*/
  40.  
  41. - (BOOL)draggedPile: aCardPile from:gameCardPileView
  42. {
  43.  
  44.     /*-----------------------------------------------------------------------
  45.      *
  46.      * Dragging is only allowed if the pile is not covered.
  47.      *
  48.      *-----------------------------------------------------------------------*/
  49.     if ([gameCardPileView pileCovered:self])
  50.     return NO;
  51.     else
  52.     return YES;
  53. }
  54.  
  55.  
  56. /*--------------------------------------------------------------------------
  57. |
  58. |    - removedPile: aCardPile from: gameCardPileView
  59. |
  60. |    returns:        (id)        self
  61. |
  62. |---------------------------------------------------------------------------
  63. |
  64. |    Called by a GamePileCardView after cards have been dragged off 
  65. |    the pile.
  66. |    
  67. \--------------------------------------------------------------------------*/
  68.  
  69. - removedPile:aCardPile from:aCardPileView
  70. {
  71.     /*-----------------------------------------------------------------------
  72.     |
  73.     |    Since the game piles only contain a single card, we
  74.     |    know that the pile is now empty, so we remove it from
  75.     |    its superview's list of subviews.  This prevents the pile
  76.     |    from getting any more mouse events.
  77.     |
  78.     \----------------------------------------------------------------------*/
  79.     
  80.     [aCardPileView removeFromSuperview];
  81.     [SolGameController() checkForWin];
  82.  
  83.     return self;
  84. }
  85.  
  86.  
  87. /*--------------------------------------------------------------------------
  88. |
  89. |    - (BOOL) canAcceptPile:aCardPile from:sender in:gameCardPileView
  90. |
  91. |    returns:    (BOOL)    YES if card pile will accept dropped cards.
  92. |
  93. |            (BOOL)    NO if this card pile rejects the dropped cards.
  94. |
  95. |---------------------------------------------------------------------------
  96. |
  97. |    Called by a GamePileCardView when the user tries to drop cards on it.
  98. |    
  99. \---------------------------------------------------------------------------*/
  100.  
  101. - (BOOL)canAcceptPile:aCardPile from:sender in:gameCardPileView
  102. {
  103.     id card1 = [aCardPile cardAt:CS_TOP];
  104.     id card2 = [[gameCardPileView cardPile] cardAt:CS_TOP];
  105.  
  106.     /*-----------------------------------------------------------------------
  107.     |
  108.     |    Can only accept a card if this pile is not covered by
  109.     |    others, and the two cards add to 13.
  110.     |
  111.     \----------------------------------------------------------------------*/
  112.     if (![gameCardPileView pileCovered:sender])
  113.     {
  114. /* #define PYRAMID_CHEAT_CHEAT_CHEAT */
  115. #ifndef PYRAMID_CHEAT_CHEAT_CHEAT
  116.     if (card1 && card2)
  117.     {
  118.         if (([card1 value] + [card2 value]) == 11)
  119.         return YES;
  120.     }
  121. #else
  122.     return YES;
  123. #endif
  124.  
  125.     }
  126.     return NO;
  127. }
  128.  
  129.  
  130. /*--------------------------------------------------------------------------
  131. |
  132. |    - acceptPile: aCardPile in: gameCardPileView
  133. |
  134. |    returns:        (id)        self
  135. |
  136. |---------------------------------------------------------------------------
  137. |
  138. |    Called by a GamePileCardView after cards have been successfully 
  139. |    added to the pile as a result of cards being dropped on it.
  140. |    
  141. \---------------------------------------------------------------------------*/
  142.  
  143. - acceptPile:aCardPile in:gameCardPileView
  144. {
  145.     id leftMatchPile = [discardCardPileViewL cardPile];
  146.     id rightMatchPile = [discardCardPileViewR cardPile];
  147.     id gamePile = [gameCardPileView cardPile];
  148.     id aCard;
  149.  
  150.     /*-----------------------------------------------------------------------
  151.     |
  152.     |    Remove both cards from the game pile, and move
  153.     |    them to the match piles.  Then remove the game pile
  154.     |    view from it's superviews list, so that it doesn't get
  155.     |    any more mouse events.
  156.     \------------------------------------------------------------------------*/
  157.  
  158.     aCard = [gamePile cardAt:CS_TOP];
  159.     [gamePile removeCard:aCard];
  160.     [leftMatchPile addCard:aCard];
  161.     aCard = [gamePile cardAt:CS_TOP];
  162.     [gamePile removeCard:aCard];
  163.     [rightMatchPile addCard:aCard];
  164.  
  165.     [discardCardPileViewL display];
  166.     [discardCardPileViewR display];
  167.     [gameCardPileView display];
  168.     [gameCardPileView removeFromSuperview];
  169.  
  170.     [SolGameController() checkForWin];
  171.     return self;
  172. }
  173.  
  174. /*--------------------------------------------------------------------------
  175. |
  176. |    doubleClickedCard:aCard in:aCardPileView
  177. |
  178. |    returns: (id) self
  179. |
  180. |---------------------------------------------------------------------------
  181. |
  182. |    Double clicked on a card; discard if it is a King.
  183. |    
  184. \--------------------------------------------------------------------------*/
  185.  
  186. - doubleClickedCard:aCard in:aCardPileView
  187. {
  188.     id cardPile = [aCardPileView cardPile];
  189.     
  190.     // can't remove a card that's covered
  191.     if ([aCardPileView pileCovered:self])
  192.     {
  193.         return self;
  194.     }
  195.  
  196.     if (aCard && (aCard == [cardPile cardAt:CS_TOP]) && 
  197.                                                [aCard value] == CS_KING)
  198.     {
  199.         [[discardCardPileViewL cardPile] addCard:[cardPile cardAt:CS_TOP]];
  200.     [cardPile removeCard:[cardPile cardAt:CS_TOP]];
  201.     [aCardPileView display];
  202.         [aCardPileView removeFromSuperview];
  203.     [discardCardPileViewL display];
  204.     [SolGameController() checkForWin];
  205.  
  206.     }
  207.     return self;
  208. }
  209.  
  210. @end
  211.